package hotel.switchs.oversea.biz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.enums.CompareRequestUrlEnums;
import hotel.base.oversea.constant.enums.CreateBasicRoomStatusEnums;
import hotel.base.oversea.constant.enums.MtRequestUrlEnums;
import hotel.base.oversea.constant.enums.SwitchBedIdEnums;
import hotel.base.oversea.dto.meituan.MtHotelGoodsDto;
import hotel.base.oversea.dto.meituan.MtHotelRoomBedInfoDto;
import hotel.base.oversea.dto.meituan.MtHotelRoomDto;
import hotel.base.oversea.entity.MtHotelRoom;
import hotel.base.oversea.entity.MtHotelRoomVo;
import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.base.oversea.utils.CompareUtil;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.StringUtil;
import hotel.base.oversea.vo.meituan.response.goods.MtHotelGoodsInventoryVo;
import hotel.switchs.oversea.api.VO.request.BasicRoomBedTypeVo;
import hotel.switchs.oversea.api.VO.request.CreateBasicRoomVo;
import hotel.switchs.oversea.api.VO.request.UpdateBasicRoomVo;
import hotel.switchs.oversea.api.entity.SwitchCreateBasicRoomEntity;
import hotel.switchs.oversea.api.entity.SwitchHotelUpEntity;
import hotel.switchs.oversea.biz.config.service.RedisService;
import hotel.switchs.oversea.biz.mapper.SwitchCreateBasicRoomMapper;
import hotel.switchs.oversea.biz.mapper.SwitchHotelUpMapper;
import hotel.switchs.oversea.biz.service.SwitchBasicRoomTypeService;
import hotel.switchs.oversea.biz.service.SwitchCreateBasicRoomService;
import hotel.switchs.oversea.biz.service.SwitchSubHotelInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @Author lmf
 * @Date 2020/6/8 14:15
 * 创建子物理房型
 **/
@Service
@Slf4j
public class SwitchCreateBasicRoomServiceImpl extends BaseServiceImpl<SwitchCreateBasicRoomEntity> implements SwitchCreateBasicRoomService {

    @Autowired
    private SwitchRestTempaleUtilsService switchRestTempaleUtilsService;
    @Autowired
    private RedisService redisService;

    @Autowired
    private SwitchRestTempaleUtilsService restTempaleUtilsService;

    //restTemplate
    //效验控制器路径
    private String COMPAER_URL = CommonUrlConstants.COMPARE_HOST+ "/";
    //美团路径
    private String METTUAN_URL = CommonUrlConstants.MEITUAN_HOST+"/";

    //物理房型相关接口
    @Autowired
    private SwitchBasicRoomTypeService switchBasicRoomTypeService;

    @Autowired
    private SwitchCreateBasicRoomMapper switchCreateBasicRoomMapper;



    //缓存数据存在
    private Map<String, SwitchCreateBasicRoomEntity> basicRoomMap = new HashMap<>();

    @Autowired
    private SwitchHotelUpMapper switchHotelUpMapper;
    @Autowired
    private SwitchSubHotelInfoService switchSubHotelInfoService;



    /**
     * 根据子酒店 母酒店 母物理房型id
     *
     * @param createBasicRoomVo
     * @return
     */
    @Override
   /* @Transactional(rollbackFor = Exception.class)*/
    public Result<Object> createBasicRoom(CreateBasicRoomVo createBasicRoomVo) {
        //黑名单过滤
        Example example=new Example(SwitchHotelUpEntity.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("hotelId",createBasicRoomVo.getChildHotelId());
        //黑名单的酒店不进行创建,上架表里没有的暂不创建
        SwitchHotelUpEntity switchHotelUpEntity=switchHotelUpMapper.selectOneByExample(example);
        if(ObjectUtil.isEmpty(switchHotelUpEntity)||switchHotelUpEntity.getUp()==2||switchHotelUpEntity.getStatus()==2){
            return new Result<>();
        }
        String key = CommonConstants.SWITCH_SELLING_THE_HOUSE_ROOM_TYPE + createBasicRoomVo.getMtHotelId() + "_" + createBasicRoomVo.getMtRoomId();
        redisService.set(key,"1");
        //判断是否已经创建过子物理房型id
        SwitchCreateBasicRoomEntity switchCreateBasicRoomEntity=switchCreateBasicRoomMapper.selectByHotelIdAndBasicIdA(createBasicRoomVo.getChildHotelId(),createBasicRoomVo.getMasterBasicRoomId());
        if(!ObjectUtils.isEmpty(switchCreateBasicRoomEntity)){
            //更新状态
            int flag=switchCreateBasicRoomMapper.updateBasicCreateStateByHotelIdAndBasicIdA(createBasicRoomVo.getChildHotelId(),createBasicRoomVo.getMasterBasicRoomId());
            log.info("===========更新创建子物理房型的创建状态为{}======",flag);
            return updateBasicRoom(createBasicRoomVo.getChildHotelId(), switchCreateBasicRoomEntity.getBasicRoomId(), createBasicRoomVo.getMasterHotelId(), createBasicRoomVo.getMasterBasicRoomIds()[0]);
        }
        Result<Object> result = switchBasicRoomTypeService.createBasicRoom(createBasicRoomVo);
        //接口调用失败
        if (result.getCode() != CommonConstants.SUCCESS) {
            log.info("=====调用母物理房型id生成子物理房型id---失败");
            return new Result<>(result.getMsg());
        }
        Integer[] basicRoomIds = (Integer[]) result.getData();
        if (basicRoomIds.length == 0) {
            log.info("====调用母物理房型id生成子物理房型，返回子物理房型数据为空====");
            return new Result<>("返回的子物理房型数据为空,错误信息为:" + result.getMsg());
        }
        int basicRoomId = basicRoomIds[0];
        //缓存失效了，就先判断表里是否有，有就跳过
        switchCreateBasicRoomEntity = new SwitchCreateBasicRoomEntity();
        switchCreateBasicRoomEntity.setChildHotelId(createBasicRoomVo.getChildHotelId());
        switchCreateBasicRoomEntity.setBasicRoomId(basicRoomId);
        SwitchCreateBasicRoomEntity switchCreateBasicRoomEntity1=switchCreateBasicRoomMapper.selectBasicRoomInfo(createBasicRoomVo.getChildHotelId(),basicRoomId);
        if(!ObjectUtils.isEmpty(switchCreateBasicRoomEntity1)){
            log.info("==========该子物理房型已经创建过,更新子物理房型信息====================");
            return updateBasicRoom(createBasicRoomVo.getChildHotelId(), basicRoomId, createBasicRoomVo.getMasterHotelId(), createBasicRoomVo.getMasterBasicRoomIds()[0]);
        }
        //没有就新增
        switchCreateBasicRoomEntity.setId(StringUtil.getUUID());
        switchCreateBasicRoomEntity.setMasterHotelId(createBasicRoomVo.getMasterHotelId());
        switchCreateBasicRoomEntity.setMasterBasicRoomId(createBasicRoomVo.getMasterBasicRoomIds()[0]);
        switchCreateBasicRoomEntity.setState(0);
        switchCreateBasicRoomEntity.setCreateState(0);
        switchCreateBasicRoomEntity.setRemark("");
        switchCreateBasicRoomEntity.setMasterHotelId(createBasicRoomVo.getMasterHotelId());
        switchCreateBasicRoomEntity.setCreateTime(new Date());
        switchCreateBasicRoomEntity.setModifyTime(new Date());
        int flag = switchCreateBasicRoomMapper.insert(switchCreateBasicRoomEntity);
        if (flag > 0) {
            log.info("======根据母物理房型生成子物理房型接口完成 end=========");
            return updateBasicRoom(createBasicRoomVo.getChildHotelId(), basicRoomId, createBasicRoomVo.getMasterHotelId(), createBasicRoomVo.getMasterBasicRoomIds()[0]);
        }else{
            return new Result<>("新增失败");
        }
    }

    /**
     * 根据子酒店id 子物理房型id
     *
     * @param hotelId
     * @param basicRoomId
     * @return
     */
    @Override
    public SwitchCreateBasicRoomEntity getCreateBasicRoom(int hotelId, int basicRoomId) {
        return switchCreateBasicRoomMapper.selectBasicRoomInfo(hotelId, basicRoomId);
    }

    /**
     * <H1>获取子酒店ID和子物理房型ID，定时器调用</H1>
     *
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @date 2020/7/14 14:52
     */
    @Override
    public Result<Object> getBasicRoomInfoForJob() {
        SwitchCreateBasicRoomEntity basicRoomEntity = switchCreateBasicRoomMapper.getBasicRoomInfoForJob();
        if(null != basicRoomEntity) {
            //设置成已经创建,并更新数据库，防止定时器调用到同一条数据
           /* SwitchCreateBasicRoomEntity entity = new SwitchCreateBasicRoomEntity();
            entity.setCreateState(1);
            Example example = new Example(SwitchCreateBasicRoomEntity.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("childHotelId", basicRoomEntity.getChildHotelId());
            criteria.andEqualTo("masterBasicRoomId", basicRoomEntity.getBasicRoomId());
            switchCreateBasicRoomMapper.updateByExampleSelective(entity, example);*/
            //设置成已经创建,并更新数据库，防止定时器调用到同一条数据
            SwitchCreateBasicRoomEntity updateEntity = new SwitchCreateBasicRoomEntity();
            updateEntity.setId(basicRoomEntity.getId());
            updateEntity.setCreateState(1);
            updateEntity.setRemark("不代表创建成功，创建中，当前为首次获取到数据改状态为1");
            switchCreateBasicRoomMapper.updateCreateStateById(updateEntity);
        }
        return new Result<>(basicRoomEntity);
    }

    @Override
    public Result<Object> updateBasicRoomInfoForJob(CreateBasicRoomVo createBasicRoomVo) {
        /*SwitchCreateBasicRoomEntity entity = new SwitchCreateBasicRoomEntity();
        entity.setCreateState(createBasicRoomVo.getCreateState());
        entity.setCreateStateRemark(createBasicRoomVo.getCreateStateRemark());
        Example example = new Example(SwitchCreateBasicRoomEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("childHotelId", createBasicRoomVo.getChildHotelId());
        criteria.andEqualTo("masterBasicRoomId", createBasicRoomVo.getMasterBasicRoomId());
        switchCreateBasicRoomMapper.updateByExampleSelective(entity, example);
        return new Result<>("更新成功!");*/
        SwitchCreateBasicRoomEntity basicRoomEntity = new SwitchCreateBasicRoomEntity();
        basicRoomEntity.setCreateState(createBasicRoomVo.getCreateState());
        basicRoomEntity.setCreateStateRemark(createBasicRoomVo.getCreateStateRemark());
        basicRoomEntity.setId(createBasicRoomVo.getId());
        switchCreateBasicRoomMapper.updateCreateStateById(basicRoomEntity);
        return new Result<>("更新成功!","success");
    }


    /**
     * 更新子物理房型
     * 母酒店id 子物理房型id
     * 1.获取到美团的映射的房型信息
     * 2.设置参数
     * 3.对比床型
     *
     * @param childHotelId      子酒店id
     * @param basicRoomId       子物理房型id
     * @param masterHotelId     母酒店id
     * @param masterBasicRoomId 母物理房型id
     * @return
     */
    private Result<Object> updateBasicRoom(int childHotelId, int basicRoomId, int masterHotelId, int masterBasicRoomId) {
        //美团房型id
        String mtRoomId = "";
        //美团酒店id
        String mtHotelId="";
        //如果缓存失效,到比较库里获取，母酒店id+母物理房型id获取对应的美团id
        Map<String, Integer> params = new HashMap<>();
        params.put("masterHotelId", masterHotelId);
        params.put("roomId", masterBasicRoomId);
        params.put("line",2);
        String url = COMPAER_URL + CompareRequestUrlEnums.GET_HOTEL_ROOM_INFO.getUrl();
        Result<MtHotelRoomVo> result = switchRestTempaleUtilsService.sendPostRequestByGetMtId(url, params);
        if (result.getCode() == CommonConstants.SUCCESS) {
            log.info("请求获取效验房型控制器成功,美团的房型信息为:{}", result.getData());
            if(!ObjectUtils.isEmpty(result.getData())){
                MtHotelRoomVo mtHotelRoomVo=result.getData();
                mtRoomId = String.valueOf(mtHotelRoomVo.getMtRoomId());
                mtHotelId= String.valueOf(mtHotelRoomVo.getMtHotelId());
            }else{
                return new Result<>("调用效验房型接口获取美团的房型信息返回为空");
            }
        } else {
            return new Result<>("调用效验房型接口获取美团的房型信息失败");
        }
        log.info("==================获取美团的房型信息=================={}");
        //获取美团的房型信息
        Map<String, Object> param2 = new HashMap<>();
        param2.put("roomId", mtRoomId);
        param2.put("mtHotelId",mtHotelId);
        url = METTUAN_URL + MtRequestUrlEnums.GET_MT_HOTEL_ROOM_BY_ID.getUrl();
        MtHotelRoom mtHotelRoom=new MtHotelRoom();
        Result result1 = restTempaleUtilsService.sendPostRequestByRoomInfo(url, param2);
        if (result1.getCode() == CommonConstants.SUCCESS) {
            log.info("请求获取美团房型控制器成功,美团信息为:{}", result1.getData());
            if(ObjectUtils.isEmpty(result1.getData())){
                return new Result<>(result1.getMsg());
            }
            MtHotelRoomDto mtHotelRoomDto = (MtHotelRoomDto) result1.getData();
            BeanUtils.copyProperties(mtHotelRoomDto, mtHotelRoom);
            log.info("获取美团房型数据成功{}", mtHotelRoom.toString());
        } else {
            log.info("调用请求获取美团房型失败");
            return new Result<>("调用请求获取美团房型失败");
        }
        //封装更新子物理房型需要的表单数据
        UpdateBasicRoomVo updateBasicRoomVo = new UpdateBasicRoomVo();
        //子酒店id
        updateBasicRoomVo.setHotelId(childHotelId);
        //最大入住人数
        String capacity = mtHotelRoom.getMtCapacity();
        updateBasicRoomVo.setPerson(Integer.valueOf(capacity));
        //是否有窗 美团0 有窗 1 部分有 2 无窗
        if(ObjectUtils.isEmpty(mtHotelRoom.getMtWindow())){
            updateBasicRoomVo.setHasWindow(0);
        }else{
            int window = mtHotelRoom.getMtWindow();
            if (window == 0) {
                updateBasicRoomVo.setHasWindow(2);
            } else if (window == 2) {
                updateBasicRoomVo.setHasWindow(0);
            } else {
                updateBasicRoomVo.setHasWindow(window);
            }
        }
        //是否可以加床 美团 0 不可加床 1 可以加床  携程:-100 未知 -1 不可加床 0 免费加床 >0 加床收费  暂时设置为免费加床
        int extraBed = ObjectUtils.isEmpty(mtHotelRoom.getMtExtraBed()) ? 0 : mtHotelRoom.getMtExtraBed();
        if (extraBed == 0) {
            updateBasicRoomVo.setAddBedFee(-1);
        } else {
            updateBasicRoomVo.setAddBedFee(0);
        }
        //是否有无线
        if(ObjectUtils.isEmpty(mtHotelRoom.getMtInternetWay())){
            updateBasicRoomVo.setHasWirelessBroadnet(2);
            updateBasicRoomVo.setHasWiredBroadnet(2);
        }else{
            int internetWay = mtHotelRoom.getMtInternetWay();
            //是否有有线 0 无法上网 1 无线WIFI 2 有线宽带 3 无线WIFI和有线宽带
            if (internetWay == 1) {
                updateBasicRoomVo.setHasWirelessBroadnet(1);
            } else if (internetWay == 2) {
                updateBasicRoomVo.setHasWiredBroadnet(1);
            } else if (internetWay == 3) {
                updateBasicRoomVo.setHasWirelessBroadnet(1);
                updateBasicRoomVo.setHasWiredBroadnet(1);
            }
        }
        log.info("============获取房型数据设置完成=================");
        log.info("==========房型数据为{}==========", updateBasicRoomVo.toString());
        //是否有床型
        //如果缓存床型信息失效从数据库获取
        List<MtHotelRoomBedInfoDto> bedList=new ArrayList<>();
        log.info("=================设置床型信息==================");
        //从数据库获取房型id获取对应的床型数据
        Map<String, Object>  param3 = new HashMap<>();
        param3.put("roomId", mtRoomId);
        param3.put("mtHotelId",mtHotelRoom.getMtHotelId());
        url = METTUAN_URL + MtRequestUrlEnums.GET_MT_HOTEL_ROOM_BED_INFO_BY_ID.getUrl();
        Result result2 = switchRestTempaleUtilsService.sendPostRequestGetBed(url, param3);
        if (result2.getCode() == CommonConstants.SUCCESS) {
            bedList = (List<MtHotelRoomBedInfoDto>) result2.getData();
            log.info("=================床型的条数为{}===============", bedList.size());
        } else {
            log.info("==============调用请求获取美团床型失败===============");
            return new Result<>("==============调用请求获取美团床型失败===================");
        }
        //携程床型信息设置
        List<BasicRoomBedTypeVo> basicRoomBedTypeVos = new ArrayList<>();
        log.info("=====开始根据美团的床型信息来设置携程=======");
        //房型类型0 大床间（单间）1 单人间（单间）2 双床间（单间）3 三人间（单间）4 套房 5 独栋 6 床位房
        //携程父床型 360 大床 361 双床 362 单人床 363多张床
        int type = mtHotelRoom.getMtRoomType();
        int bedId;
        if (bedList.size() > 0) {//如果有床型数据就进行映射设置
            for (MtHotelRoomBedInfoDto mtHotelRoomBedInfoDto : bedList) {
                BasicRoomBedTypeVo basicRoomBedTypeVo = new BasicRoomBedTypeVo();
                //房型名称
                String bedName = mtHotelRoomBedInfoDto.getMtBedType();
                //大床间
                if (type == 0) {
                    bedId = CompareUtil.getBedId(bedName, SwitchBedIdEnums.BIG.getParentId());
                    if (bedId > 0) {
                        basicRoomBedTypeVo.setParentBedTypeId(SwitchBedIdEnums.BIG.getParentId());
                        basicRoomBedTypeVo.setChildBedTypeId(bedId);
                        basicRoomBedTypeVo.setBedCount(Integer.valueOf(mtHotelRoomBedInfoDto.getMtBedCount()));
                        if(!StringUtils.isEmpty(mtHotelRoomBedInfoDto.getMtBedDesc())){
                            if(mtHotelRoomBedInfoDto.getMtBedDesc().contains("×")){
                                basicRoomBedTypeVo.setBedWidth(Double.valueOf(mtHotelRoomBedInfoDto.getMtBedDesc().split("×")[0]));
                            }
                        }
                    }
                } else if (type == 1) {//单人间
                    bedId = CompareUtil.getBedId(bedName, SwitchBedIdEnums.SINGLE.getParentId());
                    if (bedId > 0) {
                        basicRoomBedTypeVo.setParentBedTypeId(SwitchBedIdEnums.SINGLE.getParentId());
                        basicRoomBedTypeVo.setChildBedTypeId(bedId);
                        basicRoomBedTypeVo.setBedCount(Integer.valueOf(mtHotelRoomBedInfoDto.getMtBedCount()));
                        if(!StringUtils.isEmpty(mtHotelRoomBedInfoDto.getMtBedDesc())){
                            if(mtHotelRoomBedInfoDto.getMtBedDesc().contains("×")){
                                basicRoomBedTypeVo.setBedWidth(Double.valueOf(mtHotelRoomBedInfoDto.getMtBedDesc().split("×")[0]));
                            }
                        }
                    }
                } else if (type == 2) {//双床间
                    bedId = CompareUtil.getBedId(bedName, SwitchBedIdEnums.DOUBLE.getParentId());
                    if (bedId > 0) {
                        basicRoomBedTypeVo.setParentBedTypeId(SwitchBedIdEnums.DOUBLE.getParentId());
                        basicRoomBedTypeVo.setChildBedTypeId(bedId);
                        basicRoomBedTypeVo.setBedCount(Integer.valueOf(mtHotelRoomBedInfoDto.getMtBedCount()));
                        if(!StringUtils.isEmpty(mtHotelRoomBedInfoDto.getMtBedDesc())){
                            if(mtHotelRoomBedInfoDto.getMtBedDesc().contains("×")){
                                basicRoomBedTypeVo.setBedWidth(Double.valueOf(mtHotelRoomBedInfoDto.getMtBedDesc().split("×")[0]));
                            }
                        }
                    }
                } else if (type == 3) {//三人间
                    bedId = CompareUtil.getBedId(bedName, SwitchBedIdEnums.MUCH.getParentId());
                    if (bedId > 0) {
                        basicRoomBedTypeVo.setParentBedTypeId(SwitchBedIdEnums.MUCH.getParentId());
                        basicRoomBedTypeVo.setChildBedTypeId(bedId);
                        basicRoomBedTypeVo.setBedCount(Integer.valueOf(mtHotelRoomBedInfoDto.getMtBedCount()));
                        if(!StringUtils.isEmpty(mtHotelRoomBedInfoDto.getMtBedDesc())){
                            if(mtHotelRoomBedInfoDto.getMtBedDesc().contains("×")){
                                basicRoomBedTypeVo.setBedWidth(Double.valueOf(mtHotelRoomBedInfoDto.getMtBedDesc().split("×")[0]));
                            }
                        }
                    }
                }
                //说明有床型数据可以设置,若无对应的床型可以不设置
                if (!ObjectUtils.isEmpty(basicRoomBedTypeVo) && basicRoomBedTypeVo.getChildBedTypeId() != 0) {
                    basicRoomBedTypeVos.add(basicRoomBedTypeVo);
                }
            }
            //如果有对应的房型信息可以设置
            if (basicRoomBedTypeVos.size() > 0) {
                updateBasicRoomVo.setBasicRoomBedType(basicRoomBedTypeVos);
            }
        }
        updateBasicRoomVo.setBasicRoomTypeID(basicRoomId);
        log.info("====获取最终的子物理房型更新数据===={}", updateBasicRoomVo.toString());
        //3.调用更新子物理房型接口
        return switchBasicRoomTypeService.updateBasicRoom(updateBasicRoomVo);
    }
    /**
     * 效验是否可以创建子物理房型
     */
    public int isCanCreateBasicRoom(Map<String,Integer> params){
//        String access_token = HttpClientBaseAuth.getAuthToken();
      /*  MtHotelGoodsInventoryVo mtHotelGoodsInventoryVo;
        String url=METTUAN_URL+MtRequestUrlEnums.GET_MT_GOOD_INVENTERY.getUrl()+"?access_token="+access_token;
        try{
            Result<MtHotelGoodsInventoryVo> result2=RestTempaletUtils.sendPostRequestGetGoodsInventory(url,params);
            if(result2.getCode()==CommonConstants.SUCCESS){
                mtHotelGoodsInventoryVo=result2.getData();
                log.info("库存返回成功,返回的数据是{}",mtHotelGoodsInventoryVo);
            }else{
                log.info("=======库存返回失败=========,失败原因是{}",result2.getMsg());
                return false;
            }
        }catch (Exception e){
            log.info("发生的异常为{}",e.getMessage());
            return false;
        }
        if(ObjectUtils.isEmpty(mtHotelGoodsInventoryVo)){
            return false;
        }
        if(mtHotelGoodsInventoryVo.getRemain()>0){
            return true;
        }else{
            return false;
        }*/
        //2.调用美团的产品列表
        String url=CommonUrlConstants.MEITUAN_HOST+ "/"+MtRequestUrlEnums.GET_MT_GOODS_BY_HOTEL_ID.getUrl();//+"?access_token="+access_token;
        Map<String,Integer> parames=new HashMap<>();
        parames.put("hotelId",params.get("mtHotelId"));
        Map<String, List<MtHotelGoodsDto>> goodsMap;
        Result result;
        try{
            result=switchRestTempaleUtilsService.sendPostRequest(url,parames);
            log.info("======美团产品接口url为{} 美团酒店id为{}",url,params.get("mtHotelId"));
            if(result.getCode()!= CommonConstants.SUCCESS){
                log.info("==========获取美团的产品列表数据失败=============");
                result=switchRestTempaleUtilsService.sendPostRequest(url,parames);
                if(result.getCode()!= CommonConstants.SUCCESS){
                    log.info("==========获取美团的产品列表数据失败=============");
                    return CreateBasicRoomStatusEnums.CATCH.getCode();
                }else{
                    if(!ObjectUtils.isEmpty(result.getData())){
                        goodsMap= (Map<String, List<MtHotelGoodsDto>>) result.getData();
                    }else{
                        return  CreateBasicRoomStatusEnums.NO_GOODS.getCode();
                    }
                }
            }else{
                if(!ObjectUtils.isEmpty(result.getData())){
                    goodsMap= (Map<String, List<MtHotelGoodsDto>>) result.getData();
                }else{
                    return  CreateBasicRoomStatusEnums.NO_GOODS.getCode();
                }
            }
        }catch (Exception e){
            return  CreateBasicRoomStatusEnums.CATCH.getCode();
        }
        log.info("=============获取美团的产品数据成功,长度为{}================",goodsMap.size());
        if(goodsMap.size()>0&&goodsMap.keySet().size()>1){
            //判断是否有库存
            for(List<MtHotelGoodsDto> mtHotelGoodsDtos:goodsMap.values()){
                for(MtHotelGoodsDto mtHotelGoodsDto:mtHotelGoodsDtos){
                    Map<String,Object> param=new HashMap<>();
                    if(mtHotelGoodsDto.getMtRoomId().equals(String.valueOf(params.get("roomId")))){
                        param.put("roomId",mtHotelGoodsDto.getMtRoomId());
                        param.put("goodsId",mtHotelGoodsDto.getMtGoodsId());
                        param.put("days",1);
                        MtHotelGoodsInventoryVo mtHotelGoodsInventoryVo=getMtGoodsInventory(param);
                        if(ObjectUtils.isEmpty(mtHotelGoodsInventoryVo)){
                            return  CreateBasicRoomStatusEnums.CATCH.getCode();
                        }
                        if(mtHotelGoodsInventoryVo.getRemain()>0){
                            return CreateBasicRoomStatusEnums.SUCCESS.getCode();
                        }
                    }

                }
            }
            return CreateBasicRoomStatusEnums.NO_REMAIN.getCode();
        }else{
            return CreateBasicRoomStatusEnums.NO_REMAIN.getCode();
        }
    }
    /**
     * 获取美团的库存
     */
    public MtHotelGoodsInventoryVo getMtGoodsInventory(Map<String,Object> params){
//        String access_token = HttpClientBaseAuth.getAuthToken();
        MtHotelGoodsInventoryVo mtHotelGoodsInventoryVo=new MtHotelGoodsInventoryVo();
        String url=CommonUrlConstants.MEITUAN_HOST+"/"+MtRequestUrlEnums.GET_MT_GOODS_INVENTORY_BY_DAYS.getUrl();//+"?access_token="+access_token;
        try{
            Result<MtHotelGoodsInventoryVo> result2=switchRestTempaleUtilsService.sendPostRequestGetGoodsInventory(url,params);
            if(result2.getCode()==CommonConstants.SUCCESS){
                mtHotelGoodsInventoryVo=result2.getData();
                log.info("库存返回成功,返回的数据是{}",mtHotelGoodsInventoryVo);
            }else{
                log.info("=======库存返回失败=========,失败原因是{}",result2.getMsg());
            }
        }catch (Exception e){
            log.info("发生的异常为{}",e.getMessage());
            return mtHotelGoodsInventoryVo;
        }
        return mtHotelGoodsInventoryVo;
    }

    /**
     * 根据酒店id+酒店母物理房型id 获取创建物理信息
     * @param switchHotelId
     * @param switchHotelRoomId
     * @return
     */
    @Override
    public Integer updateByHotelIdAndMasterBasicRoomId(Integer switchHotelId, String switchHotelRoomId) {
        return switchCreateBasicRoomMapper.updateByHotelIdAndMasterBasicRoomId(switchHotelId,switchHotelRoomId);
    }

    /**
     * 根据子物理房型修改创建状态
     * @param basicRoomTypeId
     * @return
     */
    @Override
    public Integer updateCreateStateByBasicRoomId(Integer basicRoomTypeId) {
        return switchCreateBasicRoomMapper.updateCreateStateByBasicRoomTypeId(basicRoomTypeId);
    }
    /***
    * @Author: lmf
    * @Date: 2020/12/17
     * 随机修改房型状态
    */
    @Override
    public void updateCreateStateBasicRoomByToday() {
        switchCreateBasicRoomMapper.updateCreateStateBasicRoomByToday();
    }
}
